import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Arrays;

import javax.swing.*;
import java.awt.event.KeyListener;

public class CreateUser extends JFrame implements ActionListener, KeyListener
{

    private JPanel mainPanel = new JPanel();
	
	private JTextField usernameField= new JTextField(15); 
	private JPasswordField passwordField = new JPasswordField(15);
	private JPasswordField confirmPassword = new JPasswordField(15);

	
	private JLabel createAccountL = new JLabel("Create Account");
	private JLabel error = new JLabel("");
	private JLabel usernameL = new JLabel("Username");
	private JLabel passwordL = new JLabel("Password");
	private JLabel confirmPasswordL = new JLabel("Confirm Password:");
	
	private JButton submit = new JButton("Submit");
	
	public CreateUser()
	{
		super("Create Account For Maneno Furaha");
		
		submit.addActionListener(this);
		
		submit.addKeyListener(this);
		usernameField.addKeyListener(this);
		confirmPassword.addKeyListener(this);
		
		createAccountL.setFont(new Font("arial", Font.PLAIN, 30));
		mainPanel.add(createAccountL);
		
		error.setFont(new Font("arial", Font.BOLD, 12));
		error.setForeground(Color.RED);
		JPanel errorPane = new JPanel();
		errorPane.add(error);
		mainPanel.add(errorPane);

		JPanel panelUsername = new JPanel();
		JPanel panelPassword = new JPanel();
		JPanel panelConfirmPass = new JPanel();
		
		panelUsername.add(usernameL);
		panelUsername.add(usernameField);
		
		panelPassword.add(passwordL);
		panelPassword.add(passwordField);
		
		panelConfirmPass.add(confirmPasswordL);
		panelConfirmPass.add(confirmPassword);

		panelPassword.add(panelConfirmPass);
		
		mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
		mainPanel.add(panelUsername);
		mainPanel.add(panelPassword);
		mainPanel.add(submit);
		
		this.add(mainPanel);
		
		//this.setLocation(500,400);
		setLocationByPlatform(true);
		this.setSize(600, 400);
		this.setResizable(false);
	}
	
	public void actionPerformed(ActionEvent e)
	{

		if(!Arrays.equals(passwordField.getPassword(),confirmPassword.getPassword()))
		{
			error.setText("PASSWORDS DON'T MATCH!");
			passwordField.setText("");
			confirmPassword.setText("");
		} else if(DB.userExists(usernameField.getText()))
		{
			error.setText("USERNAME ALREADY EXSISTS!");
			passwordField.setText("");
			confirmPassword.setText("");
		} 
		else if(new String(passwordField.getPassword()).equals(""))
		{
			error.setText("PASSWORD CANNOT BE BLANK!");
		}else
		{
			try{
				DB.insertUser(usernameField.getText(), new String(passwordField.getPassword()));
				JOptionPane.showMessageDialog(this, "SUCCESS.");
				this.dispose();
			}catch (Exception a) { /*fuck off*/ }
		}

	}
	
	@Override
	public void keyPressed(KeyEvent e) 
	{	
		if(e.getKeyChar() == '\n')
			submit.doClick();
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
	}
}
